package com.mod.reanimator 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.core.Project;
	import com.calefk.util.Generic;
	import com.calefk.util.KeyPress;
	import com.calefk.util.Math2;
	import com.mod.Global;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author authorX
	 */
	public class Player extends EntitySimple 
	{
		public var hearthWorld:MovieClip;
		private var mKeyPress:KeyPress;
		private var speed:Number = 0.15;
		private var minimunHearthX:Number;
		private var maximunHearthX:Number;
		private var minimunHearthY:Number;
		private var maximunHearthY:Number;
		private var currentState:int;
		private var lastState:int;
		public static var MOVING:int = 1;
		public static var STANDING:int = 2;
		public static var INVOKING:int = 3;
		public static var DYING:int = 4;
		public static var WHILEINVOKING:int = 5;
		public static var ENDINVOKING:int = 6;
		private var minDist:Number;
		
		private var mLife:int = 100;
		
		public static const WIDTH_HEALTHBAR:int = 40;
		
		//HealthBar
		private var healthBar:MovieClip;
		
		public function Player(canvas:MovieClip, nameClip:String=null, px:Number=0, py:Number=0, project:Project=null) 
		{
			super(canvas, nameClip, px, py, project);
			mKeyPress = new KeyPress();
			mKeyPress.addKeyPress(KeyPress.KEY_W);
			mKeyPress.addKeyPress(KeyPress.KEY_S);
			mKeyPress.addKeyPress(KeyPress.KEY_A);
			mKeyPress.addKeyPress(KeyPress.KEY_D);
			currentState = Player.STANDING;
			minDist = 75 + 16;
			mClip.endInitInvoking = endInitInvoking;
			mClip.endEndInvoking = endEndInvoking;
			
			healthBar = Global.project.getMovie("barGood");
			clip.addChild(healthBar); 
			healthBar.y = -mClip.collision_mc.height / 2 - 5; 
			healthBar.x = -mClip.collision_mc.width / 2;
		}
		
		public function endEndInvoking():void 
		{
			switchState(Player.STANDING);
		}
		
		public function endInitInvoking():void 
		{
			switchState(Player.WHILEINVOKING);
			
		}
		public function switchState(newState:int):void
		{
			lastState = currentState;
			currentState = newState;
		}
		public function assigLimitsHearth():void
		{
			minimunHearthX = hearthWorld.x - hearthWorld.width / 2;
			maximunHearthX = hearthWorld.x + hearthWorld.width / 2;
			minimunHearthY = hearthWorld.y - hearthWorld.height / 2;
			maximunHearthY = hearthWorld.y + hearthWorld.height / 2;
		}
		private function verifyKeys(dt:int):void
		{
			if (currentState != DYING){
				if (mKeyPress.isKeyPress(KeyPress.KEY_W))
				{
					if (y - mClip.collision_mc.height / 2 > 0)
					{
						move(0, -speed * dt);
						if (mClip.collision_mc.hitTestObject(hearthWorld)) move(0,speed*dt);
					}
					
				}else
				if (mKeyPress.isKeyPress(KeyPress.KEY_S))
				{
					if (y + mClip.collision_mc.height / 2 < 480)
					{
						move(0, speed * dt);
						if (mClip.collision_mc.hitTestObject(hearthWorld)) move(0, -speed*dt);
					}
				}
				if (mKeyPress.isKeyPress(KeyPress.KEY_A))
				{
					if (x - mClip.collision_mc.width / 2 > 0) 
					{
						move( -speed * dt, 0);
						if (mClip.collision_mc.hitTestObject(hearthWorld)) move(speed * dt, 0);
					}
				}else 
				if (mKeyPress.isKeyPress(KeyPress.KEY_D))
				{
					if (x + mClip.collision_mc.width / 2 < 640)
					{
						move(speed * dt, 0);
						if (mClip.collision_mc.hitTestObject(hearthWorld)) move(-speed * dt, 0);
					}
				}
			}
		}
		private function movingPlayer():Boolean
		{
			return mKeyPress.isKeyPress(KeyPress.KEY_D) || mKeyPress.isKeyPress(KeyPress.KEY_S) || mKeyPress.isKeyPress(KeyPress.KEY_W) || mKeyPress.isKeyPress(KeyPress.KEY_A);
		}
		
		public override function update(dt:int):void
		{
			if (currentState != DYING)
			{
				if (currentState!=Player.INVOKING&&currentState!=Player.WHILEINVOKING&&currentState!=Player.ENDINVOKING&&movingPlayer())
					switchState(Player.MOVING);
				else
					if(currentState!=Player.INVOKING&&currentState!=Player.WHILEINVOKING&&currentState!=Player.ENDINVOKING)
						switchState(Player.STANDING);
			}
			switch(currentState)
			{
				case Player.STANDING:
					mClip.gotoAndStop("standing");
					break;
				case Player.MOVING:
					mClip.gotoAndStop("moving");
					verifyKeys(dt);
					break;
				case Player.INVOKING:
					mClip.gotoAndStop("invoking");
					break;
				case Player.WHILEINVOKING:
					mClip.gotoAndStop("whileinvoking");
				break;
				case Player.ENDINVOKING:
					mClip.gotoAndStop("invokingend");
				break;
				case Player.DYING:
					mClip.gotoAndStop("dying");
					break;
			}
			
		}
		
		 public function onKeyDown(e:KeyboardEvent):void {
            if (currentState != DYING)
				mKeyPress.onKeyDown(e);
        }
		public function onKeyUp(e:KeyboardEvent):void {
			if (currentState != DYING)
				mKeyPress.onKeyUp(e);
		}
		public function verifyCircleCollision(e:MovieClip):Boolean
		{
			var distance:Number = Math2.Pythagoras(x, y, e.x, e.y);
			if (distance < minDist)
				return true;
			return false;
		}
		
		public function onReceiveDamage(value:int):void {
			mLife += value;
			healthBar.filler_mc.x = (mLife * WIDTH_HEALTHBAR) / 100;
			if (mLife < 0) {
				currentState = DYING;
				Global.engine.heartSkin.onReceiveDamage(-Global.engine.heartSkin.mLife);
				trace("Game Over")
			}
		}
		
		
		public override function clean():void
		{
			
		}
		
	}

}